//
//  PxReaderView.h
//  Express
//
//  Created by 泰然财富 on 15/7/28.
//  Copyright (c) 2015年 泰然财富. All rights reserved.
//

#import "PxRefreshTableView.h"
#import "LLSmallUtil.h"
#import "UIView+Sizes.h"
#import "EGORefreshTableFooterView.h"
#import "EGORefreshTableHeaderView.h"

@implementation PxRefreshTableView {
    CGPoint pos;
    BOOL isPullDown;
    BOOL isDisabled;
    
    EGORefreshTableHeaderView *headerView;
    EGORefreshTableFooterView *footerView;
}

- (void) initialize
{
    pos = CGPointZero;
    isPullDown = NO;
    isDisabled = NO;
}

#define DEFAULT_HEIGHT_OFFSET 65

-(void)initViews:(CGRect) frame
{
    EGORefreshTableHeaderView *refreshTopView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, 0 - DEFAULT_HEIGHT_OFFSET, CGRectGetWidth(frame), DEFAULT_HEIGHT_OFFSET) arrowImageName:@"Images/blueArrow" textColor:[UIColor blackColor]];
    [self setTableHeaderView:refreshTopView];
    refreshTopView.delegate = self;
    headerView = refreshTopView;
    
    EGORefreshTableFooterView *refreshBottomView = [[EGORefreshTableFooterView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), DEFAULT_HEIGHT_OFFSET) arrowImageName:@"" textColor:[UIColor blackColor]];// 不要这个图片了
    [self setTableFooterView:refreshBottomView];
    refreshBottomView.delegate = self;
    footerView = refreshBottomView;
    
    self.delegate = self;
    
    self.contentInset = UIEdgeInsetsMake(-DEFAULT_HEIGHT_OFFSET, 0, 0, 0);
//    self.scrollIndicatorInsets = UIEdgeInsetsMake(-[self headerRefreshHeight], 0, 0, 0);
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
        [self initViews:frame];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self initialize];
        [self initViews:frame];
    }
    return self;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    pos = scrollView.contentOffset;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    LLDLog(@"scrollView.contentOffset.y=%f",pos.y - scrollView.contentOffset.y);
    if (pos.y - scrollView.contentOffset.y > 0) {
        [headerView egoRefreshScrollViewDidScroll:scrollView];
    } else {
        if (!isDisabled)
            [footerView egoRefreshScrollViewDidScroll:scrollView];
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (pos.y - scrollView.contentOffset.y > 0) {
        [headerView egoRefreshScrollViewDidEndDragging:scrollView];
        isPullDown = YES;
    } else {
        if (!isDisabled) {
            [footerView egoRefreshScrollViewDidEndDragging:scrollView];
            isPullDown = NO;
        }
    }
    pos = CGPointZero;
}

- (void)egoRefreshTableDidTriggerRefresh:(EGORefreshPos)aRefreshPos
{
    LLDLog(@"egoRefreshTableDidTriggerRefresh");
    if (_refreshDelegate) {
        [_refreshDelegate egoRefreshTableDidTriggerRefresh:aRefreshPos];
    }
}

- (BOOL)egoRefreshTableDataSourceIsLoading:(UIView*)view
{
    LLDLog(@"egoRefreshTableDataSourceIsLoading");
    if (_refreshDelegate) {
        return [_refreshDelegate egoRefreshTableDataSourceIsLoading:view];
    }
    return NO;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_refreshDelegate) {
        [_refreshDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_refreshDelegate) {
        [_refreshDelegate tableView:tableView didDeselectRowAtIndexPath:indexPath];
    }
}

-(void)refreshDone:(EGORefreshPos)aRefreshPos
{
    if (aRefreshPos == EGORefreshHeader) {
        [headerView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
        self.contentInset = UIEdgeInsetsMake(-DEFAULT_HEIGHT_OFFSET, 0, 0, 0);
    } else {
        [footerView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
    }
}

-(void) setFootViewEnable:(BOOL)isEnable
{
    isDisabled = !isEnable;
    [footerView setHidden:isDisabled];
}

@end